//
//  MyNameChangeCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/21.
//  Copyright © 2016年 game. All rights reserved.
//

#import "MyNameChangeCtrl.h"
#import "MBButtonWithFontAdapter.h"
#import "MBLabelWithFontAdapter.h"
#import "MBTextFieldWithFontAdapter.h"
#import "MBTextViewWithFontAdapter.h"
@interface MyNameChangeCtrl ()
@property (weak, nonatomic) IBOutlet MBLabelWithFontAdapter *navTitle;
@property (weak, nonatomic) IBOutlet MBTextFieldWithFontAdapter *textField;

@end

@implementation MyNameChangeCtrl
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}

- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.frame = [UIScreen mainScreen].bounds;
    self.view.backgroundColor = tabGropBackColor;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ViewTap)];
    [self.view addGestureRecognizer:tap];
    
       self.textField.text = _text;
    
    if (self.isAddName) {
        self.navTitle.text = @"输入用户名完成注册";
        self.textField.placeholder = @"请输入1~5个汉字或者2~16个字母或数字";
    }
}

-(void)ViewTap{
    
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

- (BOOL)validatePassword{
    
        NSString * number = @"^\\w*\\d+\\w*$";   //数字
        NSString * lower = @"^\\w*[a-z]+\\w*$";   //小写字母
        NSString * upper = @"^\\w*[A-Z]+\\w*$";  //大写字母
        NSString * word = @"^\\w*[\u4E00-\u9FA5]+\\w*$";
   
    return  ([self validateWithRegExp: number] || [self validateWithRegExp: lower] || [self validateWithRegExp: upper] || [self validateWithRegExp: word]);
}

- (BOOL)validateWithRegExp: (NSString *)regExp
{
    NSPredicate * predicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", regExp];
    return [predicate evaluateWithObject: self.textField.text];
}

- (IBAction)tijiaoClick:(MBButtonWithFontAdapter *)sender {
    
     [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    
    if (self.isAddName) {
        
        NSString *testString = self.textField.text;
        NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];

        if ((testData.length<16&&testData.length>2)&&[self validatePassword]) {
            if ([self validatePassword]) {
                [self ChangeNameWhithAction:@"check_username"];
            }

        }else{
            [Manager GetHudWhithText:@"请输入1~5个汉字或者2~16个字母或数字" AndFatherView:self.view];
        }
    }else{
        
        [self ChangeNameWhithAction:@"modify_info"];
     }
}


-(void)ChangeNameWhithAction:(NSString *)action{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic addEntriesFromDictionary:@{
                                    
                                    @"version":@"4",
                                    @"module":@"member",
                                    @"action":action,
                                    @"choice":@"username",
                                    @"username":self.textField.text,
                                    }];
    
    [[Manager sharedManager]ResponseWhithURL:@"http://api.diaoyur.cn/app/index.php" AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
  
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            if (!self.isAddName) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            
           __weak typeof(self) weakSelf = self;
            if (_ChangeBlock) {
                _ChangeBlock(weakSelf.textField.text);
            }
        }else{
            
            [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.view];
        }
        
    }];
}
@end